class control {
  // 量算操作
  burette() {
    Cesium.tileSize = 256
    Cesium.Ion.defaultAccessToken = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiI5MWVkMjllNy03ZjY4LTQ3YmQtOTgxOC05NGQ5YTU0ZjM5ZGEiLCJpZCI6MzU5Nywic2NvcGVzIjpbImFzciIsImdjIl0sImlhdCI6MTUzODE5MTUyM30.dtS2F3-q2fGoA93N7cFl-LCikK-Rjk7v01WWA-RqCxg'

    var viewer = new Cesium.Viewer('cesiumContainer', {
      selectionIndicator: false,
      useBrowserRecommendedResolution: false,
      animation: false, //是否显示动画控件
      baseLayerPicker: false, //是否显示图层选择控件
      geocoder: false, //是否显示地名查找控件
      timeline: false, //是否显示时间线控件
      sceneModePicker: true, //是否显示投影方式控件
      navigationHelpButton: false, //是否显示帮助信息控件
      infoBox: false, //是否显示点击要素之后显示的信息
      imageryProvider: false,
      fullscreenButton: true,
    })
    viewer.scene.globe.preloadAncestors = false
    viewer.scene.primitives.destroyPrimitives = false
    viewer.scene.globe.depthTestAgainstTerrain = true
    var imageryLayers = viewer.imageryLayers
    Cesium.viewer = viewer

    var scene = viewer.scene

    //天地图影像图层
    var tiandituImageLayer = new Cesium.ImageryLayer(
      new Cesium.WebMapTileServiceImageryProvider({
        url: 'http://{s}.tianditu.gov.cn/img_w/wmts?service=wmts&request=GetTile&version=1.0.0' + '&LAYER=img&tileMatrixSet=w&TileMatrix={TileMatrix}&TileRow={TileRow}&TileCol={TileCol}' + '&style=default&format=tiles&tk=440b96d736fad95da7934bc130a2258a',
        layer: 'tdtImg_c',
        style: 'default',
        format: 'tiles',
        tileMatrixSetID: 'c',
        subdomains: ['t0', 't1', 't2', 't3', 't4', 't5', 't6', 't7'],
        tilingScheme: new Cesium.WebMercatorTilingScheme(),
        tileMatrixLabels: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16',
          '17', '18', '19'
        ],
        maximumLevel: 20,
      })
    )
    viewer.imageryLayers.add(tiandituImageLayer)

    var material = Cesium.Material.fromType("PolylineDash", {
      color: new Cesium.Color(1.0, 0.0, 1.0, 1.0),
      dashLength: 25,
    });

    var primitive = new Cesium.Primitive({
      geometryInstances: new Cesium.GeometryInstance({
        geometry: new Cesium.PolylineGeometry({
          positions: Cesium.Cartesian3.fromDegreesArray([
            119.58176980474877,
            25.7362,
            119.88176980474877,
            25.7362,
          ]), //坐标必须两个和两个以上
          width: 5.0, //线宽
          vertexFormat: Cesium.PolylineMaterialAppearance.VERTEX_FORMAT,
        }),
      }),
      appearance: new Cesium.PolylineMaterialAppearance({
        material: material,
        translucent: false,
      }),
    });
    scene.primitives.add(primitive);

    viewer.scene.postProcessStages._fxaa.enabled = true;
    viewer.camera.setView({
      destination: Cesium.Cartesian3.fromDegrees(
        119.58176980474877,
        25.7362,
        4500.0
      ), //设置位置
      orientation: {
        heading: Cesium.Math.toRadians(0.0),
        pitch: Cesium.Math.toRadians(-60),
        roll: 0,
      },
    });
    handler = new Cesium.ScreenSpaceEventHandler(viewer.canvas);
    measureLineSpace(viewer, handler)


    function measureLineSpace(viewer, handler) {
      // 取消双击事件-追踪该位置
      viewer.cesiumWidget.screenSpaceEventHandler.removeInputAction(
        Cesium.ScreenSpaceEventType.LEFT_DOUBLE_CLICK
      );

      handler = new Cesium.ScreenSpaceEventHandler(
        viewer.scene._imageryLayerCollection
      );
      var positions = [];
      var poly = null;
      var distance = 0;
      var cartesian = null;
      var floatingPoint;

      handler.setInputAction(function (movement) {
        let ray = viewer.camera.getPickRay(movement.endPosition);
        cartesian = viewer.scene.globe.pick(ray, viewer.scene);
        if (positions.length >= 2) {
          if (!Cesium.defined(poly)) {
            poly = new PolyLinePrimitive(positions);
          } else {
            positions.pop();
            positions.push(cartesian);
          }
          distance = getSpaceDistance(positions);
        }
      }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);

      handler.setInputAction(function (movement) {
        let ray = viewer.camera.getPickRay(movement.position);
        cartesian = viewer.scene.globe.pick(ray, viewer.scene);
        if (positions.length == 0) {
          positions.push(cartesian.clone());
        }
        positions.push(cartesian);
        var textDisance = distance + "米";
        floatingPoint = viewer.entities.add({
          name: "空间直线距离",
          position: positions[positions.length - 1],
          point: {
            pixelSize: 5,
            color: Cesium.Color.RED,
            outlineColor: Cesium.Color.WHITE,
            outlineWidth: 2,
          },
          label: {
            text: textDisance,
            font: "18px sans-serif",
            fillColor: Cesium.Color.GOLD,
            style: Cesium.LabelStyle.FILL_AND_OUTLINE,
            outlineWidth: 2,
            verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
            pixelOffset: new Cesium.Cartesian2(20, -20),
          },
        });
      }, Cesium.ScreenSpaceEventType.LEFT_CLICK);

      handler.setInputAction(function (movement) {
        handler.destroy(); //关闭事件句柄
        positions.pop(); //最后一个点无效
      }, Cesium.ScreenSpaceEventType.RIGHT_CLICK);

      var PolyLinePrimitive = (function () {
        function _(positions) {
          this.options = {
            name: "直线",
            polyline: {
              show: true,
              positions: [],
              width: 10,
              clampToGround: true,
            },
          };
          this.positions = positions;
          this._init();
        }

        _.prototype._init = function () {
          var _self = this;
          var _update = function () {
            return _self.positions;
          };
          //实时更新polyline.positions
          this.options.polyline.positions = new Cesium.CallbackProperty(
            _update,
            false
          );
          viewer.entities.add(this.options);
        };

        return _;
      })();

      //空间两点距离计算函数
      function getSpaceDistance(positions) {
        var distance = 0;
        for (var i = 0; i < positions.length - 1; i++) {
          var point1cartographic = Cesium.Cartographic.fromCartesian(
            positions[i]
          );
          var point2cartographic = Cesium.Cartographic.fromCartesian(
            positions[i + 1]
          );
          /**根据经纬度计算出距离**/
          var geodesic = new Cesium.EllipsoidGeodesic();
          geodesic.setEndPoints(point1cartographic, point2cartographic);
          var s = geodesic.surfaceDistance;
          //返回两点之间的距离
          s = Math.sqrt(
            Math.pow(s, 2) +
            Math.pow(
              point2cartographic.height - point1cartographic.height,
              2
            )
          );
          distance = distance + s;
        }
        return distance.toFixed(2);
      }
    }
  }

  // 鼠标位置
  location() {
    Cesium.tileSize = 256
    Cesium.Ion.defaultAccessToken = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiI5MWVkMjllNy03ZjY4LTQ3YmQtOTgxOC05NGQ5YTU0ZjM5ZGEiLCJpZCI6MzU5Nywic2NvcGVzIjpbImFzciIsImdjIl0sImlhdCI6MTUzODE5MTUyM30.dtS2F3-q2fGoA93N7cFl-LCikK-Rjk7v01WWA-RqCxg'

    var viewer = new Cesium.Viewer('cesiumContainer', {
      selectionIndicator: false,
      useBrowserRecommendedResolution: false,
      animation: false, //是否显示动画控件
      baseLayerPicker: false, //是否显示图层选择控件
      geocoder: false, //是否显示地名查找控件
      timeline: false, //是否显示时间线控件
      sceneModePicker: true, //是否显示投影方式控件
      navigationHelpButton: false, //是否显示帮助信息控件
      infoBox: false, //是否显示点击要素之后显示的信息
      imageryProvider: false,
      fullscreenButton: true,
    })
    viewer.scene.globe.preloadAncestors = false
    viewer.scene.primitives.destroyPrimitives = false
    viewer.scene.globe.depthTestAgainstTerrain = true
    var imageryLayers = viewer.imageryLayers
    Cesium.viewer = viewer

    var scene = viewer.scene

    //天地图影像图层
    var tiandituImageLayer = new Cesium.ImageryLayer(
      new Cesium.WebMapTileServiceImageryProvider({
        url: 'http://{s}.tianditu.gov.cn/img_w/wmts?service=wmts&request=GetTile&version=1.0.0' + '&LAYER=img&tileMatrixSet=w&TileMatrix={TileMatrix}&TileRow={TileRow}&TileCol={TileCol}' + '&style=default&format=tiles&tk=440b96d736fad95da7934bc130a2258a',
        layer: 'tdtImg_c',
        style: 'default',
        format: 'tiles',
        tileMatrixSetID: 'c',
        subdomains: ['t0', 't1', 't2', 't3', 't4', 't5', 't6', 't7'],
        tilingScheme: new Cesium.WebMercatorTilingScheme(),
        tileMatrixLabels: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19'],
        maximumLevel: 20,
      })
    )
    viewer.imageryLayers.add(tiandituImageLayer)

    // 设置视图
    viewer.camera.setView({
      destination: Cesium.Cartesian3.fromDegrees(119.57160055699248, 25.70102836786316, 383.6698647688256),
      orientation: {
        heading: Cesium.Math.toRadians(340.0876405649615),
        pitch: Cesium.Math.toRadians(-9.965309016806488),
        roll: Cesium.Math.toRadians(359.94808234298637),
      },
    })

    // 监听鼠标点击事件
    var handler = new Cesium.ScreenSpaceEventHandler(scene.canvas);
    handler.setInputAction(function (movement) {
      // 鼠标点击屏幕距canvas左上角距离
      $('.coordinates1').text(movement.position)
      // 地球表面的坐标
      var canvas = scene.canvas;
      var center = new Cesium.Cartesian2(canvas.clientWidth / 2.0, canvas.clientHeight / 2.0);
      var ellipsoid = scene.globe.ellipsoid;
      var result = viewer.camera.pickEllipsoid(center, ellipsoid);
      $('.coordinates2').text(result)
      // 经纬度
      let cartesian = scene.pickPosition(movement.position);
      let cartographic = Cesium.Cartographic.fromCartesian(cartesian);
      let lng = Cesium.Math.toDegrees(cartographic.longitude);
      let lat = Cesium.Math.toDegrees(cartographic.latitude);
      $('.coordinates3').text(`${lng}, ${lat}`)
    }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
  }
}
